package cn.hp.controller;

import cn.hp.entity.Announcement;
import cn.hp.service.AnnouncementService;
import cn.hp.util.Result;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/announcement")
public class AnnouncementController {
    @Autowired
    private AnnouncementService announcementService;
    @GetMapping
    public  Result find(Announcement announcement){
        List<Announcement> list=announcementService.findAnnouncement(announcement);
        return Result.success(list);
    }
    @RequestMapping("/page")
    public Result page(Announcement announcement, @RequestParam(value = "page",defaultValue = "1") Integer page, @RequestParam(value = "limit") Integer limit){
        PageInfo<Announcement> announcementPageInfo=announcementService.findAnnouncement(announcement,page,limit);
        return Result.success(announcementPageInfo);
    }
    @PostMapping
    public Result add(@RequestBody Announcement announcement){
        int i=announcementService.insertAnnouncement(announcement);
        return i>0 ?Result.success("添加成功") : Result.error("添加失败");
    }
    @PutMapping
    public Result update(@RequestBody Announcement announcement){
        int i=announcementService.updateAnnouncement(announcement);
        return i>0 ?Result.success("修改成功") : Result.error("修改失败");
    }
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        int i=announcementService.deleteAnnouncement(id);
        return i>0 ?Result.success("删除成功") : Result.error("删除失败");
    }

}
